// const createApp = require('/path/to/built-server-bundle.js')
// const Koa = require('koa')

const { createBundleRenderer } = require('vue-server-renderer')
const template = require('fs').readFileSync('./template.html', 'utf-8')
const serverBundle = require('../dist-server/vue-ssr-server-bundle.json')
const clientManifest = require('../dist-client/vue-ssr-client-manifest.json')

// const server = new Koa();
var express = require('express');
var server = express();
const renderer = createBundleRenderer(serverBundle, {
  runInNewContext: false, // 推荐
  template, // （可选）页面模板
  clientManifest // （可选）客户端构建 manifest
})

server.get('*', (req, res) => {
  const context = {
    url: req.url,
    title: 'hello',
    meta: `
      <meta ...>
      <meta ...>
    `
  }

  renderer.renderToString(context, (err, html) => {
    if (err) {
      if (err.code === 404) {
        res.status(404).end('Page not found')
      } else {
        res.status(500).end('Internal Server Error')
      }
    } else {
      res.end(html)
    }
  })
})

server.listen(3000, function () {
  var host = this.address().address;
  var port = this.address().port;
  console.log('Example app listening at http://%s:%s', host, port);
});
